package com.soup.lab.basic.iso;

import com.segi.uhomecp.park.access.ctr.common.ConfigurableService;
import com.segi.uhomecp.park.access.ctr.common.PropertyConfigurer;
import org.springframework.beans.factory.InitializingBean;

public class IsolateTaskSchedulerService implements ConfigurableService, InitializingBean {

    private String name;

    private IsolateTaskScheduler scheduler;

    private PropertyConfigurer configurer;

    public IsolateTaskSchedulerService(String name, IsolateTaskScheduler scheduler) {
        this.name = name;
        this.scheduler = scheduler;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        start();
        configurer = new PropertyConfigurer();
        configurer.configurable("steal", param -> scheduler.setSteal(Boolean.parseBoolean(param)))
                .viewable("running", () -> String.valueOf(scheduler.isStarted()))
                .viewable("steal", () -> String.valueOf(scheduler.isSteal()));
    }

    @Override
    public void start() {
        scheduler.start();
    }

    @Override
    public void stop() {
        scheduler.stop();
    }

    @Override
    public void restart() {
        scheduler.stop();
        scheduler.start();
    }

    @Override
    public String name() {
        return name;
    }

    @Override
    public String status() {
        if(configurer != null) {
            return configurer.view();
        }
        return "";
    }

    @Override
    public void config(String key, String val) {
        if(configurer != null) {
            configurer.config(key, val);
        }
    }

}
